<?php

require_once ('Site.class.php');
require_once ('Event.class.php');

class EventManager {

    private $connection;

    const ADD_EVENT = "INSERT INTO events (start, end, description, all_day, title, type_id) VALUES (:start, :end, :description, :all_day, :title, :type_id);";
    const DELETE_EVENT = "DELETE FROM events WHERE id = :id;";
    const UPDATE_EVENT = "UPDATE events SET start = :start, end = :end, description = :description, all_day  = :all_day, title = :title, type_id = :type_id WHERE id = :id;";
    const GET_EVENT_BY_ID = "SELECT events.id, title, events.description, events.start, events.end, all_day, type_id, events_types.description AS type_description, bg_color, text_color FROM events INNER JOIN events_types ON events.type_id = events_types.id WHERE events.id = :id;";
    const GET_EVENTS_BY_DAY = "SELECT events.id, title, events.description, events.start, events.end, all_day, type_id, events_types.description AS type_description, bg_color, text_color FROM events INNER JOIN events_types ON events.type_id = events_types.id WHERE (DATE(events.start) = :date) OR ((DATE(events.end) >= :date) AND (DATE(events.start) < :date)) ORDER BY start;";
    const GET_EVENTS_TYPES = "SELECT * FROM events_types;";
    const GET_ALL_EVENTS = "SELECT events.id, title, events.description, events.start, events.end, all_day, type_id, events_types.description AS type_description, bg_color, text_color FROM events INNER JOIN events_types ON events.type_id = events_types.id WHERE MONTH(events.start) = :month AND YEAR(events.start) = :year ORDER BY events.start DESC";
    
    const GET_TYPES = "SELECT id, description FROM events_types;";
    
    public function __construct() {
        $this->connection = new PDO(
                        "mysql:host=" . Site::DB_HOST . ";dbname=" . Site::DB_NAME,
                        Site::DB_USERNAME,
                        Site::DB_PASSWORD,
                        array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
        );
    }

    public function add_event(&$e) {
        $stmt = $this->connection->prepare(self::ADD_EVENT);
        $stmt->execute(array(
            'start' => $e->start->format('Y-m-d H:i:s'),
            'end' => $e->end->format('Y-m-d H:i:s'),
            'title' => $e->title,
            'description' => $e->description,
            'all_day' => ($e->all_day) ? 1 : 0,
            'type_id' => $e->type_id
        ));
        $e->id = $this->connection->lastInsertId();
    }

    public function delete_event($id) {
        $stmt = $this->connection->prepare(self::DELETE_EVENT);
        $stmt->execute(array('id' => $id));
        return $stmt->rowCount();
    }

    public function update_event($e) {
        $stmt = $this->connection->prepare(self::UPDATE_EVENT);
        $stmt->execute(array(
            'id' => $e->id,
            'start' => $e->start->format('Y-m-d H:i:s'),
            'end' => $e->end->format('Y-m-d H:i:s'),
            'title' => $e->title,
            'description' => $e->description,
            'all_day' => ($e->all_day) ? 1 : 0,
            'type_id' => $e->type_id
        ));
        return $stmt->rowCount();
    }
    
    public function get_all_events($month, $year) {
        $stmt = $this->connection->prepare(self::GET_ALL_EVENTS);
        $stmt->execute(array('month' => $month, 'year' => $year));
        $result = array();
        $event = null;
        while ($e = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $event = new Event();
            foreach ($e as $key => $value) {
                if (($key == 'start') || ($key == 'end'))
                    $event->{$key} = new DateTime($value);
                else
                    $event->{$key} = $value;
            }
            array_push($result, $event);
        }
        unset($stmt);
        return $result;
    }
    public function get_event_by_id($id) {
        $stmt = $this->connection->prepare(self::GET_EVENT_BY_ID);
        $stmt->execute(array('id' => $id));
        $event = null;
        if ($e = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $event = new Event();
            foreach ($e as $key => $value) {
                if (($key == 'start') || ($key == 'end'))
                    $event->{$key} = new DateTime($value);
                else if ($key == 'all_day') {
                    $event->{$key} = ($value == 1) ? TRUE : FALSE;
                } else {
                    $event->{$key} = $value;
                }
            }
        }

        return $event;
    }

    public function get_community_week_events() {
        $date = new DateTime("now");
        $stmt = $this->connection->prepare(self::GET_EVENTS_BY_DAY);
        $result = array();
        for ($i = 0; $i < 8; $i++) {
            $d = $date->format('Y-m-d');
            $result[$d] = array();
            $stmt->execute(array('date' => $d));
            while ($e = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $event = new Event();
                foreach ($e as $key => $value) {
                    if (($key == 'start') || ($key == 'end'))
                        $event->{$key} = new DateTime($value);
                    else
                        $event->{$key} = $value;
                }
                array_push($result[$d], $event);
            }

            $date->add(new DateInterval("P1D"));
        }

        return $result;
    }
    
    public function get_events_styles() {
        $stmt = $this->connection->prepare(self::GET_EVENTS_TYPES);
        $stmt->execute();
        ?>
        <style>
        <?php
            while($et = $stmt->fetch(PDO::FETCH_ASSOC)) {
                ?>
                div.label-<?= $et['description'] ?> {
                    background-color: <?= $et['bg_color'] ?>;
                    color: <?= $et['text_color'] ?>;
                }
                
                <?php
            } 
        ?>
        </style>
        <?php

    }
    
    public function get_types_select($current_id) {
        $stmt = $this->connection->prepare(self::GET_TYPES);
        $stmt->execute();
        ?>
        <select name="type" id="type">
            <option <?= ($current_id == null) ? 'selected' : '' ?> value="no-selection">Seleziona Catagoria</option>
        <?php
            while ($t = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
            <option value="<?= $t['id']?>" <?= ($current_id != null) && ($current_id == $t['id']) ? 'selected' : '' ?>><?= $t['description'] ?></option>
            <?php
            }
            ?>
        </select>
        <?php
    }

}

?>